package com.example.fengyuwuzu2.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class Tools {
    /**
     * 日期格式化
     */
    public static String customFormat(Date date, String pattern) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern, Locale.CHINA);
        return simpleDateFormat.format(date);
    }

    public static String getJson(Context context) {//从assets下读取user.json
        // 将json数据变成字符串
        StringBuilder stringBuilder = new StringBuilder();
        // 获得assets资源管理器
        AssetManager assetManager = context.getAssets();
        // 使用IO流读取json文件内容
        try {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(
                    assetManager.open("news.json"), "utf-8"));
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                stringBuilder.append(line.trim());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }

    /**
     * 日期字符串转为另一种格式日期字符串
     *
     * @param time
     * @return
     * @throws ParseException
     */
    public static String StringToDate(String time, String oldPattern, String newPattern) throws ParseException {

        SimpleDateFormat format = new SimpleDateFormat(oldPattern, Locale.CHINA);
        Date date;
        date = format.parse(time);
        SimpleDateFormat format1 = new SimpleDateFormat(newPattern, Locale.CHINA);
        String s = format1.format(date);
        return s;
    }


    /**
     * 分享纯文本
     *
     * @param content 内容
     */
    public static void shareString(Activity activity, String content) {
        Intent sendIntent = new Intent();
        sendIntent.setAction(Intent.ACTION_SEND);
        sendIntent.putExtra(Intent.EXTRA_TEXT, content);//分享的文本内容
        sendIntent.setType("text/plain");
        activity.startActivity(Intent.createChooser(sendIntent, "分享到"));
    }
}
